package com.doyoo.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.doyoo.enums.State;
import com.doyoo.enums.Type;
import io.swagger.annotations.ApiModel;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 定时任务
 */

/**
 * @Accessors(fluent = true, chain = true) 中文含义是存取器, 作用于getter/setter
 * 1. chain  表示链式, 返回当前对象
 * 2. fluent 表示流畅, 返回当前对象, 但是getter和setter使用的都是属性名,不会是getId, setId
 * 3. prefix 表示前缀, 指的是忽略指定的前缀, 比如 pId, 生成如getId/setId, 忽略掉指定'p'
 * 注意: 使用fluent会导致反序列化框架报错, 因为找不到setter方法
 *
 * @EqualsAndHashCode(callSuper = true) 将父类属性纳入重写, 避免子类属性值相同, 但父类属性值不同
 *
 * @ToString(callSuper = true) 将父类中的属性也算到tostring中
 *
 */
@Data
@EqualsAndHashCode(callSuper = false)
@ApiModel(value = "JobInfo对象", description = "定时任务")
public class JobInfo implements Serializable {

    private static final long serialVersionUID = 1L;

    @Schema(description = "自增id")
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @NotEmpty(message = "'name'不能为空")
    @Pattern(regexp = "^[a-z][a-z-]{0,61}[a-z]$", message = "'name'格式不正确，只能包含小写字母和连字符")
    @Schema(description = "定时任务名称，cronjob.metadata.name")
    private String name;

    @NotNull(message = "'type'不能为空")
    @Schema(description = "类型，1 shell，2 python")
    private Type type;

    @Schema(description = "描述")
    private String description;

    @NotEmpty(message = "'envs'不能为空")
    @Pattern(regexp = "^[a-z][a-z-]{0,61}[a-z]$", message = "'envs'格式不正确，只能包含小写字母和连字符")
    @Schema(description = "环境，逗号隔开")
    private String envs;

    @NotEmpty(message = "'schedule'不能为空")
    @Schema(description = "cron表达式 cronjob.spec.schedule")
    private String schedule;

    @Schema(description = "成功记录保留数，cronjob.spec.successfulJobsHistoryLimit")
    private Integer successfulHistoryLimit;

    @Schema(description = "失败记录保留数，cronjob.spec.failedJobsHistoryLimit")
    private Integer failedHistoryLimit;

    @TableField(insertStrategy = FieldStrategy.NEVER)
    @Schema(description = "状态，0初始，1启用，2禁用")
    private State state;

    @Schema(description = "联系人，逗号分隔")
    private String contacts;

    @Schema(description = "创建人账号")
    private String createUser;

    @Schema(description = "创建时间")
    @TableField(insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER)
    private LocalDateTime createTime;

    @Schema(description = "更新时间")
    @TableField(insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER)
    private LocalDateTime updateTime;

    @NotEmpty(message = "'stepList'不能为空")
    @TableField(exist = false)
    private List<JobStep> stepList;

    public String getTypeName() {
        for (Type e : Type.values()) {
            if (e.equals(this.getType())) {
                return e.getName();
            }
        }
        return null;
    }

    public String getStateDescription() {
        for (State e : State.values()) {
            if (e.equals(this.getState())) {
                return e.getName();
            }
        }
        return null;
    }
}
